package org.duang.logs;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.duang.config.Config;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Level;

public abstract class Logger {

	protected static boolean isSend = true;
	protected static ILogFactory factory;
	private static final ConcurrentMap<String, Logger> logbackMapping = new ConcurrentHashMap<String, Logger>();
	
//	static {
//		init();
//	}
	
	public static Logger getLogger(Class<?> clazz) {
		Logger logger = logbackMapping.get(clazz.getName());
		if(null == logger){
			if(null == factory) init();
			logger = factory.getLogger(clazz);
			logbackMapping.putIfAbsent(clazz.getName(), logger);
		}
		return logger;
	}
	
	public static void init(String level, String logbackClassName) {
		if (null != factory)
			return;
		try {			
			factory= (ILogFactory)Class.forName(logbackClassName).newInstance();
			ch.qos.logback.classic.Logger rootLogger =  (ch.qos.logback.classic.Logger)LoggerFactory.getILoggerFactory().getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);
			rootLogger.setLevel(Level.toLevel(level));
			rootLogger.setAdditive(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void init() {
		if (null != factory)
			return;
		try {
			String className = Config.getLogBackFactoryExample();
			factory= (ILogFactory)Class.forName(className).newInstance();
			ch.qos.logback.classic.Logger rootLogger =  (ch.qos.logback.classic.Logger)LoggerFactory.getILoggerFactory().getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);
			rootLogger.setLevel(Level.toLevel(Config.getLogBackLevel()));
			rootLogger.setAdditive(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void stop() {
		logbackMapping.clear();
	}
	
	/**
	 *  只对需要提交到数据库的日志处理器进行是否发送控制<br/>
	 *  默认true为发送
	 */
	public void isSend(boolean send) {
		isSend = send;
	}
	public abstract void print(String message);

	public abstract void print(String msg, Throwable t);

//	public abstract void info(String message);
//	
//	public abstract void info(String message, Throwable t);
//	
//	public abstract void warn(String message);
//	
//	public abstract void warn(String message, Throwable t);
//	
//	public abstract void error(String message);
//	
//	public abstract void error(String message, Throwable t);
	
	public abstract boolean isDebugEnabled();

	public abstract boolean isInfoEnabled();

	public abstract boolean isWarnEnabled();

	public abstract boolean isErrorEnabled();
}
